package com.xms.form.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.core.model.PagedList;
import com.xms.form.entity.SystemForm;
import com.xms.form.service.ISystemFormFinderService;
import com.xms.ribbonbutton.core.RibbonButtonArea;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.ribbonbutton.service.IRibbonButtonFinderService;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@RestController
@RequestMapping("{org}/api/schema/form")
public class SystemFormFinderController extends ApiBaseController {
    private ISystemFormFinderService _systemFormFinderService;
    private IRibbonButtonFinderService _ribbonButtonFinderService;
    @Autowired
    public SystemFormFinderController(ISystemFormFinderService systemFormFinderService
            , IRibbonButtonFinderService ribbonButtonFinderService) {
        _systemFormFinderService = systemFormFinderService;
        _ribbonButtonFinderService = ribbonButtonFinderService;
    }
    @ApiOperation("表单列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public JsonResultObject get(@RequestParam(name="entityid", required = true) UUID entityId,
                                @RequestParam(name="name", required = false) String name,
                                @RequestParam(name="getall", required = false) Boolean getall,
                                @RequestParam(name="page", required = false) Integer page,
                                @RequestParam(name="pagesize", required = false) Integer pagesize,
                                @RequestParam(value="sortby", required = false) String sortby,
                                @RequestParam(value="sortdirection", required = false) String sortdirection)
    {
        if (UUIDUtil.isEmpty(entityId))
        {
            return JNotSpecifiedParams("entityid");
        }

        LambdaQueryWrapper<SystemForm> lambdaQueryWrapper = getSortedQueryWrapper(sortby, sortdirection);
        lambdaQueryWrapper.eq(SystemForm::getEntityid, entityId);
        if (StringUtil.notEmpty(name))
        {
            lambdaQueryWrapper.like(SystemForm::getName, name);
        }
        if (getall != null && getall) {
            return JOk(_systemFormFinderService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            return JNotSpecifiedParams("page","pagesize");
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<SystemForm> datas = _systemFormFinderService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        PagedList<SystemForm> result = new PagedList<>(datas.getRecords(), page, pagesize, datas.getTotal(), datas.getPages());

        return JOk(result);
    }

    @ApiOperation("查询表单")
    @GetMapping({"getbyid/{id}"})
    public JsonResultObject getById(@PathVariable(value="id", required = true) UUID id) {
        SystemForm result = _systemFormFinderService.getById(id);
        if (result == null) {
            return JNotFound();
        }
        return JOk(result);
    }

    @ApiOperation("查询表单")
    @GetMapping("getbyentityid")
    public JsonResultObject getByEntityId(@RequestParam(value="entityid", required = true) UUID entityId,
                                          @RequestParam(value="authorized", required = false) Boolean authorized) {
        List<SystemForm> result = null;
        if (authorized != null && authorized) {
            result = _systemFormFinderService.getAuthorizedByEntityId(entityId);
        } else {
            result = _systemFormFinderService.getByEntityId(entityId);
        }
        return JOk(result);
    }

    @ApiOperation("查询表单")
    @GetMapping("getbyentityname")
    public JsonResultObject getByEntityName(@RequestParam(value="entityname", required = true) String entityName,
                                            @RequestParam(value="authorized", required = false) Boolean authorized) {
        List<SystemForm> result = null;
        if (authorized != null && authorized) {
            result = _systemFormFinderService.getAuthorizedByEntityName(entityName);
        } else {
            result = _systemFormFinderService.getByEntityName(entityName);
        }
        return JOk(result);
    }

    @ApiOperation("查询表单按钮")
    @GetMapping("getbuttons/{id}")
    public JsonResultObject getButtons(@PathVariable(value="id", required = true) UUID id) {
        SystemForm form = _systemFormFinderService.getById(id);
        if (form == null) {
            return JNotFound();
        }
        List<RibbonButton> buttons = _ribbonButtonFinderService.getByArea(form.getEntityid(), RibbonButtonArea.Form);
        if (form.getIscustombutton() && StringUtil.notEmpty(form.getCustombuttons())) {
            List<UUID> buttonid = JsonUtil.toList(form.getCustombuttons(), UUID.class);
            buttons.removeIf(x -> !buttonid.contains(x.getRibbonbuttonid()));
        }
        return JOk(buttons);
    }
}
